package com.shujia.scala

import com.shujia.scala.Demo15CaseClass.StuCaseClass

object Demo20Match {
  def main(args: Array[String]): Unit = {

    /**
     * Scala中的模式匹配：match
     * 类似Java中的switch
     * 1、匹配值
     * 2、匹配字符串
     * 3、匹配枚举
     * 4、匹配类型
     * 5、匹配样例类对象
     */

    // 1、匹配值
    val age: Int = 18

    age match {
      case 18 =>
        println("永远18岁")
        println("永远18岁")
        println("永远18岁")
        println("永远18岁")
        println("永远18岁")
      case 20 =>
        println("20岁")
      case _ =>
        println("其它")

    }
    // 2、匹配字符串
    val language: String = "Scala"
    language match {
      case "Scala" =>
        println("Scala是一门多范式的编程语言，既可以面向对象编程，也可以面向函数")
      case "Java" =>
        println("Java是一门面向对象的编程语言")
      case "Python" =>
        println("Python是一门动态数据类型的解释型语言")
    }

    // 3、匹配枚举
    val tuesday: Week.Value = Week.Tuesday
    tuesday match {
      case Week.Tuesday =>
        println("星期二马上要放假了")
      case Week.Wednesday =>
        println("星期三自习")
      case _ =>
        println("其它情况")
    }

    // 4、匹配类型
    val x: Any = 20

    x match {
      case i: Int =>
        println("变量x是int类型")
      case s: String =>
        println("变量x是String类型")
      case _ =>
        println("其他情况")
    }

    // 001,zs,20,男,文科一班
    val stuList: List[(String, (String, (Int, (String, String))))] = List(("001", ("zs", (20, ("男", "文科一班")))), ("002", ("zs2", (22, ("男", "文科2班")))), ("003", ("ls", (21, ("男", "文科一班")))))

    stuList.map(t2 => {
      val id: String = t2._1
      val name: String = t2._2._1
      val age: Int = t2._2._2._1
      val gender: String = t2._2._2._2._1
      val clazz: String = t2._2._2._2._2
      s"$id,$name,$age,$gender,$clazz"
    }).foreach(println)

    // 模式匹配在map操作的使用，可以简化取值的过程
    stuList.map {
      case (id: String, (name: String, (age: Int, (gender: String, clazz: String)))) =>
        s"$id,$name,$age,$gender,$clazz"
    }.foreach(println)

    // 5、匹配样例类对象
    val zs: StuCaseClass = StuCaseClass("02", "zs", 20, "女", "一班")

    zs match {
      case StuCaseClass("01", "zs", 20, "男", "一班") =>
        println("男zs")
      case StuCaseClass("01", "zs", 20, "女", "一班") =>
        println("女zs")
      // 如果匹配不上会报错
      case _ =>
        println("其他情况")
    }


  }

}

// 定义枚举
object Week extends Enumeration {
  val Monday = Value("mon.")
  val Tuesday = Value("tue.")
  val Wednesday = Value("wed.")
  val Thursday = Value("thur.")
  val Friday = Value("fri.")
  val Saturday = Value("sa.")
  val Sunday = Value("sun.")
}
